Kwik.Page.include("Kwik.Ui.Controls.Control.js");

/** 
* Titlebar for form objects
* @constructor
* @extends Kwik.Ui.Controls.Control
* @requires Kwik.Ui.Controls.Control
* @requires Kwik.Ui.Controls.Form
*/
Kwik.Ui.Controls.FormTitleBar = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Ui.Controls.Control");
	this.__class.push("Kwik.Ui.Controls.FormTitleBar");

	/** Title element @type HTMLSpan */
	this.title = null;
	/** Icon element @type HTMLImg */
	this.icon = null;
	/** Minimize button element @type HTMLImg */
	this.min = null;
	/** Maximize button element @type HTMLImg */
	this.max = null;
	/** Exit buttin element @type HTMLImg */
	this.exit = null;

	/** Load the properties from an element source */
	this.load = function(element)
	{
		this._removeEvents();

		if(element.parentNode!=null){element.parentNode.removeChild(element);}

		var icons = {icon: this.icon, min: this.min, max: this.max, exit: this.exit};
		for(e in icons)
		{
			var img = Kwik.Dom.Utility.getChildElements(element, "img", e)[0];
			if(img)
			{
				icons[e].parentNode.replaceChild(img, icons[e]);
				img.style.cursor = "default";
				this[e] = img;
			}	
		}
		var title = Kwik.Dom.Utility.getElementsByClassName(element, "title")[0];
		if(title){this.title.innerHTML = title.innerHTML;}
		this._addEvents();
	};

	/** Configure this object */
	this.configure= function FormTitleBar$configure(config)
	{
		if(config && config.element){config.element = null;}
		config = this.Kwik$Ui$Controls$Control.configure(config);

		Kwik.Page.log.write("Kwik.Ui.Controls.FormTitleBar.configure: Setting the formtitlebar specific items from config.");

		if(!Kwik.Type.isUndefined(config.icon)){this.icon.style.display = "inline"; this.icon.src = config.icon;}
		if(!Kwik.Type.isUndefined(config.min)){this.min.style.display = "inline"; this.min.src = config.min;}
		if(!Kwik.Type.isUndefined(config.max)){this.max.style.display = "inline"; this.max.src = config.max;}
		if(!Kwik.Type.isUndefined(config.exit)){this.exit.style.display = "inline"; this.exit.src = config.exit;}
		if(!Kwik.Type.isUndefined(config.title)){this.title.innerHTML = config.title;}
	};

	/** Add events to the button elements */
	this._addEvents = function()
	{
		if(!this._eventMin)
		{
			this._eventMin = new Function(this.referenceString() + ".fireEvent(\"onmin\");");
			this._eventMax = new Function(this.referenceString() + ".fireEvent(\"onmax\");");
			this._eventExit = new Function(this.referenceString() + ".fireEvent(\"onexit\");");
			this._eventReturn = function(e){e = e||event; e.cancelBubble = true; return true;};
		}

		Kwik.Utility.attachEvent(this.min, "onclick", this._eventMin);
		Kwik.Utility.attachEvent(this.max, "onclick", this._eventMax);
		Kwik.Utility.attachEvent(this.exit, "onclick", this._eventExit);

		Kwik.Utility.attachEvent(this.min, "onmousedown", this._eventReturn);
		Kwik.Utility.attachEvent(this.max, "onmousedown", this._eventReturn);
		Kwik.Utility.attachEvent(this.exit, "onmousedown", this._eventReturn);
	};

	/** Remove the events from the button objects */
	this._removeEvents = function()
	{
		if(!this._eventMin){return;}

		Kwik.Utility.detachEvent(this.min, "onclick", this._eventMin);
		Kwik.Utility.detachEvent(this.max, "onclick", this._eventMax);
		Kwik.Utility.detachEvent(this.exit, "onclick", this._eventExit);

		Kwik.Utility.detachEvent(this.min, "onmousedown", this._eventReturn);
		Kwik.Utility.detachEvent(this.max, "onmousedown", this._eventReturn);
		Kwik.Utility.detachEvent(this.exit, "onmousedown", this._eventReturn);
	};

	/** Event fired when minimize button is clicked */
	this.onmin = function(){};
	/** Event fired when maximize button is clicked */
	this.onmax = function(){};
	/** Event fired when exit button is clicked */
	this.onexit = function(){};

	/** @ignore */
	this.main = function FormTitleBar_main(config)
	{
		this.element = document.createElement("table");
		var tbody = this.element.appendChild(document.createElement("tbody"));
		var tr = tbody.appendChild(document.createElement("tr"));
		this._td1 = tr.appendChild(document.createElement("td"));
		this._td2 = tr.appendChild(document.createElement("td"));

		this.element.className = this.getCssClassName();
		this.element.cellSpacing = 0;
		this.element.cellPadding = 0;
		this._td1.className = "Title";
		this._td2.className = "ControlBox";

		this.icon = this._td1.appendChild(document.createElement("img"));
		this.title = this._td1.appendChild(document.createElement("span"));
		this.min = this._td2.appendChild(document.createElement("img"));
		this.max = this._td2.appendChild(document.createElement("img"));
		this.exit = this._td2.appendChild(document.createElement("img"));

		this.icon.style.cssText = "display:none;";
		this.min.style.cssText = "display:none; cursor:default;";
		this.max.style.cssText = "display:none; cursor:default;";
		this.exit.style.cssText = "display:none; cursor:default;";

		this._addEvents();

		this.configure(config);
	}; this.main(config);
};